package core_masterworker;

import mpi.MPI;

public abstract class Master {
	/* Attributes of master */
	private int np;
	private int nworkers;
	private int id;
	private double[] start_time = new double[2];
	private double[] end_time = new double[2];
	
	/* Constructor */
	public Master(int nworkers_){
		nworkers = nworkers_;
	}
	
	public Master(){
		//nworkers = np;
	}
	
	public void setNumberOfWorker(int nworker){
		if(nworker > (np - 1)){
			System.out.println("The number of worker should be no more than " + (np-1) );
			nworkers = np - 1;
		}
		else
			nworkers = np - 1;
	}
	
	public void startMPI(String[] args){
		MPI.Init(args);
		id = MPI.COMM_WORLD.Rank();
		np = MPI.COMM_WORLD.Size();
		
		System.out.println("\n\t Processor " + id + " cheking...");	  
		MPI.COMM_WORLD.Barrier();
		  
		/* Check master worker id */
		if(id == 0){
			System.out.println("\n\t Master process id = " +id);
		}
		else{
			System.out.println("\n\t Worker process id = " +id);
		}	  
		MPI.COMM_WORLD.Barrier();
	}
	
	public int getID(){
		return id;
	}
	public int getNumberOfWorkers(){
		return nworkers;
	}
	public int getNumberOfProcessors(){
		return np;
	}
	
	public void startTime(){
		 start_time[0] = MPI.Wtime();
		 start_time[1] = System.currentTimeMillis();
	}
	public void endTime(){
		end_time[0] = MPI.Wtime();
		end_time[1] = System.currentTimeMillis();
	}
	
	/* Get time of MPI */
	public double[] getStarTime(){
		return start_time;
	}
	
	/* Get time of system */
	public double[] getEndTime(){
		return end_time;
	}	
	
	
	/* Process results */
	abstract public void processResult();
	
	/* Prepare work */
	abstract public void prepareWork();
}
